import {
    getBookOrderList,
    approvalBookOrder,
    deleteBookOrderById,
    getBookOrderById,
    getApprovaList
} from '../../services/mobile/bookorder';
import Constants from '../../utils/Constants';
import {message} from "antd";
import OperatorConstant from "../../utils/OperatorConstant";
const R = require('ramda');

export default {
    namespace: 'bookorder',
    state: {
        page: {
            page: 1,
            size: 10,
            totalRow: 0,
            totalPage: 0
        },

        list: [],
        approvalList: [],
        dataSource: {},
        bookorderStatus: "1",
    },
    reducers: {

        //-----------------setList

        /**
         * 设置开票人列表
         * @param state
         * @param payload
         * @returns {{drawerList: *, drawerPage: (state.drawerPage|{page, size, totalRow, totalPage}|*)}}
         */
        setBookorderList(state, {payload}) {
            return {...state, list: payload.list, page: payload.page, bookorderStatus: payload.status}
        },

        /**
         * 设置审核记录
         * @param state
         * @param payload
         * @returns {{approvalList: (Array|*)}}
         */
        setApprovalList(state, {payload}) {
            return {...state, approvalList: payload.approvalList}
        },


        //--------------------setEntity


        /**
         * 设置开票人
         * @param state
         * @param action
         * @returns {{drawer: (state.drawer|{}|*)}}
         */
        setBookorder(state, action) {
            let payload = action.payload;
            return {...state, dataSource: payload.dataSource};
        },


    },
    effects: {

        /**
         * 获取预约下单列表
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *getBookOrderList({payload}, {call, put, select}) {
            let page = yield select(state => state.bookorder.page);
            page = R.isNil(payload) || R.isNil(payload.page) ? page : payload.page;

            let status = yield select(state => state.bookorder.bookorderStatus);
            status = R.isNil(payload) || R.isNil(payload.status) ? status : payload.status;


            const res = yield call(getBookOrderList, {
                productname: R.isNil(payload) || R.isNil(payload.productname) ? "" : payload.productname,
                customerno: R.isNil(payload) || R.isNil(payload.customerno) ? "" : payload.customerno,
                companyname: R.isNil(payload) || R.isNil(payload.companyname) ? "" : payload.companyname,
                status: status,
                page
            });


            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                yield put({
                    type: 'setBookorderList',
                    payload: {
                        list: R.isNil(content.list) ? [] : content.list,
                        page: R.isNil(content.page) ? Constants.DEFAULTPAGE : content.page,
                        status: status,
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },


        /**
         * 获取预约下单详情
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *getBookOrderById({payload}, {call, put, select}) {
            const res = yield call(getBookOrderById, {id: payload.id});

            if (res.data.resultCode === "ok") {
                let data = res.data.content;
                yield put({
                    type: "setBookorder",
                    payload: {
                        dataSource: data,
                    }
                });

            }
        },


        /**
         * 删除预约下单
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *deleteBookOrderById({payload}, {call, put, select}) {
            const res = yield call(deleteBookOrderById, {id: payload.id});
            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                message.info("删除预约下单成功");

                if (payload.onSuccess())payload.onSuccess();

            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 审核预约下单
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *approvalBookOrder({payload}, {call, put, select}) {
            const res = yield call(approvalBookOrder, payload);
            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                message.info("预约下单审核成功");
                if (payload.onSuccess())payload.onSuccess();

            } else {
                message.error(res.data.errMessage);
            }
        },


        /**
         * 审核记录
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *getApprovaList({payload}, {call, put, select}) {

            const res = yield call(getApprovaList, payload);
            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                yield put({
                    type: 'setApprovalList',
                    payload: {
                        approvalList: R.isNil(content) ? [] : content,
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },

    },
    subscriptions: {
        setup({history, dispatch}) {

        }
    }
};